{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "07ccf61f-0991-4963-b0e8-8ce74657901b",
   "metadata": {},
   "source": [
    "# 2.TensorFlow 2语法基础"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a79ef54-63e6-429f-ba9f-590b736a7f29",
   "metadata": {},
   "source": [
    "## 2.1 使用tf.constant方法创建张量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "- 使用数值5创建张量\n",
    "- 使用数值5创建张量，并使用shape指定其形状为(2,4)。\n",
    "- 使用列表[[1,2],[3,4]]创建张量，并使用dtype指定其数据类型为float32。\n",
    "- 使用NumPy数组np.array([1.,2.])创建张量。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8d7baa9c-93a2-42f3-a3c1-231cdb587f2d",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74ad989a-9b82-43e1-b841-e74284cd5936",
   "metadata": {},
   "source": [
    "### 3.任务分析\n",
    "\n",
    "张量由Tensor类实现，每个张量都是一个Tensor对象，tf.constant方法格式如下"
   ]
  },
  {
   "cell_type": "raw",
   "id": "558f6098-1729-43fb-aef7-f72f83b5cc71",
   "metadata": {},
   "source": [
    "tf.constant(\n",
    "    value, dtype=None, shape=None\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e5588f6-e8e6-4f3e-aeb8-a25fdd8d6cdb",
   "metadata": {},
   "source": [
    "参数说明如下：\n",
    "\n",
    "- value：张量的值，可以是数值、Python列表或NumPy数组。\n",
    "- dtype: 元素的数据类型。\n",
    "- shape: 张量的形状。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(5, shape=(), dtype=int32)\n",
      "tf.Tensor(\n",
      "[[5 5 5 5]\n",
      " [5 5 5 5]], shape=(2, 4), dtype=int32)\n",
      "tf.Tensor(\n",
      "[[1. 2.]\n",
      " [3. 4.]], shape=(2, 2), dtype=float32)\n",
      "tf.Tensor([1 2], shape=(2,), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "# value为5\n",
    "re1=tf.constant(value=5)\n",
    "print(re1)\n",
    "# value为5，形状为(2,4)\n",
    "re2=tf.constant(value=5,shape=(2,4))\n",
    "print(re2)\n",
    "# value为Python列表，指定类型\n",
    "re3=tf.constant(value=[[1,2],[3,4]],dtype=tf.float32)\n",
    "print(re3)\n",
    "# value为NumPy数组\n",
    "re4=tf.constant(value=np.array([1,2]))\n",
    "print(re4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "281ab916-c0a6-4b6c-b84c-21f65488b1db",
   "metadata": {},
   "source": [
    "输出分析：可通过shape属性观察张量的形状。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "745e27eb-80a5-43fe-acba-d5ddd5fa4de9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
